<?php

namespace App\Http\Controllers\API\Admin;

use App\Http\Requests\API\CreateFreightAPIRequest;
use App\Http\Requests\API\UpdateFreightAPIRequest;
use App\Models\Freight;
use App\Models\FreightArea;
use App\Repositories\FreightRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use Illuminate\Support\Facades\Auth;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class FreightController
 * @package App\Http\Controllers\API
 */

class FreightAPIAdminController extends ApiBaseController
{
    /** @var  FreightRepository */
    private $freightRepository;

    protected $model_name = 'App\Models\Freight';

    public function __construct(FreightRepository $freightRepo)
    {
        $this->freightRepository = $freightRepo;
    }

    public function authLimit()
    {
        $shop = Auth::guard('admin')->user()->shop;
        $res = [['status',1]];
        $res[] =  ['shop_id', $shop->id];
        return $res;
    }

    public function _after_find(&$data)
    {
        foreach ($data->areas as &$area) {
            $area->provinces;
        }
    }

    public function _after_get($data)
    {
        $data->load('areas');
        foreach ($data as &$v) {
            foreach ($v->areas as &$area) {
                $area->provinces;
            }
        }
        return $data;
    }


    public function beforeStore($id, $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $freight = Freight::find($id);
        if(isset($request['areas'])){
            $areas = $request['areas'];
            if(is_string($areas)){
                $areas = json_decode($areas,true);
            }
        }else{
            $areas = [];
        }
        foreach ($areas as $area) {
            $area['shop_id'] = $shop->id;
            $area['freight_id'] = $id;
            $new_area = FreightArea::create($area);
            $_province_ids = $area['province_ids'];
            if(is_string($_province_ids) && $_province_ids){
                $_province_ids = explode(',', $_province_ids);
            }
            $new_area->provinces()->sync($_province_ids);
        }
        $freight->shop_id = $shop->id;
        $freight->save();
        return responseZK(1,$freight);
    }

    public function beforeUpdate($id, $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $freight = Freight::find($id);
        if(isset($request['areas'])){
            $areas = $request['areas'];
            if(is_string($areas)){
                $areas = json_decode($areas,true);
            }
        }else{
            $areas = [];
        }
        $has_area_ids = [];
        foreach ($areas as $area) {
            $_province_ids = $area['province_ids'];
            unset($area['province_ids']);
            $area['shop_id'] = $shop->id;
            $area['freight_id'] = $id;
            if(isset($area['id'])){
                $has_area_ids[] = $area['id'];
                FreightArea::where('id',$area['id'])->update($area);
                $new_area = FreightArea::find($area['id']);
            }else{
                $new_area = FreightArea::create($area);
                $has_area_ids[] = $new_area['id'];
            }
            if(is_string($_province_ids) && $_province_ids){
                $_province_ids = explode(',', $_province_ids);
            }
            $new_area->provinces()->sync($_province_ids);
        }
        FreightArea::where('freight_id',$id)->whereNotIn('id',$has_area_ids)->delete();
        $freight->shop_id = $shop->id;
        $freight->save();
    }
}
